
AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )
include( 'shared.lua' )

if SERVER then
resource.AddFile("materials/mesh_cloth/blackjeans.vtf")
resource.AddFile("materials/mesh_cloth/blackjeans.vmt")
resource.AddFile("materials/mesh_cloth/bluejeans.vtf")
resource.AddFile("materials/mesh_cloth/bluejeans.vmt")
resource.AddFile("materials/mesh_cloth/desertcamo.vtf")
resource.AddFile("materials/mesh_cloth/desertcamo.vmt")
resource.AddFile("materials/mesh_cloth/greencamo.vtf")
resource.AddFile("materials/mesh_cloth/greencamo.vmt")
end

// -------------------------------------------------------------
// Mesh Cloth Maker MeshConfig - v0.5 - Quad version
// Made By: Suicidal.Banana
// -------------------------------------------------------------
// Disclaimer: You are free to use bits of code from my sent, 
// as long as you GIVE ME CREDIT, Suicidal.Banana
// -------------------------------------------------------------

/*---------------------------------------------------------
   Funcions: Create
---------------------------------------------------------*/
function ENT:SpawnFunction( ply, tr )

	if ( !tr.Hit ) then return end
	
	local SpawnPos = tr.HitPos + tr.HitNormal * 16
	local ent = ents.Create( "MeshCloth_MeshConfig" )
	ent:SetPos( SpawnPos )
	ent:Spawn()
	ent:Activate()
	return ent
	
end

function ENT:KeyValue (key, value)

    self[key] = tonumber(value) or value
    
end 

/*---------------------------------------------------------
   Funcions: Initialize
---------------------------------------------------------*/
function ENT:Initialize()
	
	self.Entity:PhysicsInit(SOLID_VPHYSICS);
	self.Entity:SetMoveType(MOVETYPE_VPHYSICS);
	self.Entity:SetSolid(SOLID_VPHYSICS);
	self.Entity:SetModel("models/props/cs_italy/orange.mdl") 
	// Don't use the model's physics - create a sphere instead
	self.Entity:PhysicsInitSphere( 4, "metal_bouncy" )
	
	// freeze
	local phys = self.Entity:GetPhysicsObject()
	if (phys:IsValid()) then
		phys:EnableMotion(false)
	end
	
	// Set collision bounds exactly
	self.Entity:SetCollisionBounds( Vector( -2, -2, -2 ), Vector( 2, 2, 2 ) )
	self.Entity:SetColor( 0, 100, 255, 150 )

end	
	
function ENT:createthemesh()

	local plyid = self.theplyid
	local xmax = self.thelastx
	local ymax = self.thelasty
	local clothcount = self.theclothcount
	
	

	for countx=0, xmax do
		for county=0, ymax do
			if countx > 0 and county > 0 then
	
							local Asphere_name2 = "MeshClothSphere_" .. plyid .. "_count" .. clothcount .. "_x" .. countx .. "_y" .. county
							local Asphere_check2 = ents.FindByName( Asphere_name2 )
		
								for CountA, Asphere1 in pairs(Asphere_check2) do 
									theentnr = tonumber(Asphere1:EntIndex())
									--print("pos1: " .. theentnr)
								end
								
								
							local xsphere_name2 = "MeshClothSphere_" .. plyid .. "_count" .. clothcount .. "_x" .. countx .. "_y" .. county - 1
							local xsphere_check2 = ents.FindByName( xsphere_name2 )
		
								for Countx, xsphere1 in pairs(xsphere_check2) do 
									pos2 = tonumber(xsphere1:EntIndex())
									--print("pos2: " .. pos2)
								end
	
							local ysphere_name2 = "MeshClothSphere_" .. plyid .. "_count" .. clothcount .. "_x" .. countx - 1 .. "_y" .. county - 1
							local ysphere_check2 = ents.FindByName( ysphere_name2 )
		
								for County, ysphere1 in pairs(ysphere_check2) do 
									pos3 = tonumber(ysphere1:EntIndex())
									--print("pos3: " .. pos3)
								end
		
							local zsphere_name2 = "MeshClothSphere_" .. plyid .. "_count" .. clothcount .. "_x" .. countx - 1 .. "_y" .. county
							local zsphere_check2 = ents.FindByName( zsphere_name2 )
		
								for Countz, zsphere1 in pairs(zsphere_check2) do 
									pos4 = tonumber(zsphere1:EntIndex())
									--print("pos4: " .. pos4)
								end
								
								local Qsphere_name2 = "MeshClothSphere_" .. plyid .. "_count" .. clothcount .. "_x" .. countx .. "_y" .. county
								local Qsphere_check2 = ents.FindByName( Qsphere_name2 )

								for CountQ, Qsphere1 in pairs(Qsphere_check2) do
									local meshheight = tonumber(self.meshheight) / 2
									local meshdisplay = self.meshdisplay
									local meshmat = self.meshmaterial
									Qsphere1:GetTable():SetMeshData( theentnr, pos2, pos3, pos4, meshheight, meshdisplay, meshmat )
									--print("Did it lol")
								end

					end
					end
					end
			self.Entity:Remove()
end

/*---------------------------------------------------------
   Funcions: Collisions
---------------------------------------------------------*/

function ENT:PhysicsCollide( data, physobj )

end

/*---------------------------------------------------------
   Funcions: OnTakeDamage and Touch
---------------------------------------------------------*/

function ENT:OnTakeDamage()

end


function ENT:Touch(ent)

end

/*---------------------------------------------------------
   Funcions: Use and Think
---------------------------------------------------------*/
function ENT:Use( activator, caller )

end

function ENT:Think()

end


